/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.common.controllers;

import be.contribute.common.pojo.Temperature;
import be.contribute.common.pojo.TextMessage;
import be.contribute.common.utils.StringUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author debian
 */
public class PeripheralController {

    private static PeripheralController fPeriphalController;
    private final Integer MSG_SYSTEM_OFFLINE = 0;
    private final Integer MSG_ACTION_COMPLETE = 1;
    private final Integer MSG_SYSTEM_ONLINE = 2;
    
    public PeripheralController() {
    }
    
    public static PeripheralController getInstance()
    {
        if(fPeriphalController == null)
            fPeriphalController = new PeripheralController();
        return fPeriphalController;
    }
   
    public List<Temperature> readOwnTemperature() {
        List<Temperature> readList = new ArrayList();
        try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("sudo /home/pi/wiringPi/wiringPi/./sensor");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

            //Read the output from the command
            String s;            
            while ((s = stdInput.readLine()) != null) 
            {
                System.out.println("Jav: "+s);        
                Double temp_value = StringUtil.getDoubleFromString(s, "T");
                Double hum_value = StringUtil.getDoubleFromString(s, "H");
                Integer room_id = StringUtil.getIntFromString(s, "R");
                
                if(temp_value != null && hum_value != null)
                    readList.add(new Temperature(1,room_id,temp_value, hum_value, new Date()));
            }
            // read any errors from the attempted command
            while ((s = stdError.readLine()) != null) 
            {
                System.out.println("Error:" + s);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return readList;
    }   
    
    public boolean initGSM()
    {
        try {
            Runtime rt = Runtime.getRuntime();
                //Process pr = rt.exec("sudo python initGSM.py"); //Python version, less stable
                Process pr = rt.exec("sudo /home/pi/arduPi/./initGSM");
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

                //Read the output from the command
                String s;            
                while ((s = stdInput.readLine()) != null) 
                {
                    System.out.println("GSM: "+s);
                    if(s.equals("INIT DONE"))
                    {
                        return true;
                    }
                }
                // read any errors from the attempted command
                while ((s = stdError.readLine()) != null) 
                {
                    System.out.println("Error:" + s);
                }
        } catch (IOException ex) {
            Logger.getLogger(PeripheralController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public List<TextMessage> readTexts()
    {
        List<TextMessage> vList = new ArrayList();
        try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("sudo python readGSM.py");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

            //Read the output from the command
            String s;    
            TextMessage msg = null;
            boolean messageToRead = false;
            String buffer = null;
            
            while ((s = stdInput.readLine()) != null) 
            {
                 if(s.equals("READ DONE"))
                {
                    System.out.println("READING TEXTS IS COMPLETED");
                    return vList;
                }

                if(messageToRead)
                {
                    if(buffer != null)
                    {
                        if(buffer.length() < s.length())
                            msg.setMessage(s);
                        else
                            msg.setMessage("");

                        vList.add(msg);
                        messageToRead = false;
                    }
                    else
                    {
                        buffer = s;
                    }
                }

                if(s.indexOf("MGL:") > 0 && !messageToRead)
                {
                    messageToRead = true;
                    msg = StringUtil.getTextMessageFromString(s);
                    buffer = null;
                }
            
            }
            // read any errors from the attempted command
            while ((s = stdError.readLine()) != null) 
            {
                //System.out.println("Error:" + s);
            }
        } catch (IOException ex) {
            return vList;
        }
        return vList;
    }
    
    public boolean sendText(Integer pCode)
    {
        try
        {    
            Runtime rt = Runtime.getRuntime();
            Process pr = null;

            if(MSG_ACTION_COMPLETE.equals(pCode))
                pr = rt.exec("sudo /home/pi/arduPi/./sendCompleteSMS");
            if(MSG_SYSTEM_ONLINE.equals(pCode))
                pr = rt.exec("sudo /home/pi/arduPi/./sendOnlineSMS");
            if(MSG_SYSTEM_OFFLINE.equals(pCode))
                pr = rt.exec("sudo /home/pi/arduPi/./sendOfflineSMS");
        
            if(pr != null)
                return true;
        }
        catch(IOException ex)
        {
            return false;
        }
        return false;
    }
}